import React from 'react';
import {bindModel, bindAction, render, getModel, getAction} from '../../../index';
import Modal from 'antd/lib/modal';
import Action from './Action';
import Model from './Model';


export default ({modelName, actionName}) => {

    // 如果没有绑定过  就绑定默认的
    if (!getModel(modelName)) {
        bindModel({
            [modelName]: Model
        });
    }
    if (!getAction(actionName)) {
        bindAction({
            [actionName]: Action
        });
    }

    return render({
        models: [modelName],
        actions: [actionName]
    })(class extends React.Component {
        constructor(props) {
            super(props);

            if (typeof props.action[actionName].init === 'function') {
                props.action[actionName].init(modelName, actionName);
            }
        }
        render() {
            const {children} = this.props;
            const model = this.props[modelName];
            const actionInst = this.props.action[actionName];

            let otherProps = {};

            Object.keys(this.props).forEach((key) => {
                if (key !== modelName && key !== 'action' && key !== 'children') {
                    otherProps[key] = this.props[key];
                }
            });

            return (
                <Modal
                    {...otherProps}
                    title={model.title}
                    visible={model.visible}
                    onCancel={actionInst.close}
                    footer={actionInst.renderFooter()}
                    width={model.width}
                    maskClosable={model.maskClosable}
                >
                    {children}
                </Modal>
            );
        }
    })

}
